package org.xxpay.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.entity.PayAlizftMchInfo;
import org.xxpay.core.entity.PayAlizftMchInfoExample;
import org.xxpay.core.service.IPayAlizftMchInfoService;
import org.xxpay.service.dao.mapper.PayAlizftMchInfoMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: pangxiaoyu
 * @date: 2020/10/21
 * @description:
 */
@Service
public class PayAlizftMchInfoServiceImpl implements IPayAlizftMchInfoService {

    @Autowired
    private PayAlizftMchInfoMapper payAlizftMchInfoMapper;

    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    @Override
    public int add(PayAlizftMchInfo payAlizftMchInfo) {
        return payAlizftMchInfoMapper.insertSelective(payAlizftMchInfo);
    }

    @Override
    public int update(PayAlizftMchInfo payAlizftMchInfo) {
        return payAlizftMchInfoMapper.updateByPrimaryKeySelective(payAlizftMchInfo);
    }

    @Override
    public int delete(Long id) {
        return payAlizftMchInfoMapper.deleteByPrimaryKey(id);
    }


    @Override
    public List<PayAlizftMchInfo> select(int offset, int limit, PayAlizftMchInfo payAlizftMchInfo) {
        PayAlizftMchInfoExample example = new PayAlizftMchInfoExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        PayAlizftMchInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payAlizftMchInfo);
        return payAlizftMchInfoMapper.selectByExample(example);
    }

    @Override
    public long count(PayAlizftMchInfo payAlizftMchInfo) {
        PayAlizftMchInfoExample example = new PayAlizftMchInfoExample();
        PayAlizftMchInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payAlizftMchInfo);
        return payAlizftMchInfoMapper.countByExample(example);
    }

    @Override
    public PayAlizftMchInfo findById(Long subMchId) {
        return payAlizftMchInfoMapper.selectByPrimaryKey(subMchId);
    }

    @Override
    public int updateByCondition(PayAlizftMchInfo updateRecord, PayAlizftMchInfo condition, List<Byte> applyStatusList) {
        PayAlizftMchInfoExample example = new PayAlizftMchInfoExample();
        PayAlizftMchInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, condition);

        if(applyStatusList !=null && !applyStatusList.isEmpty()){ //查询多个申请状态
            criteria.andApplyStatusIn(applyStatusList);
        }

        return payAlizftMchInfoMapper.updateByExampleSelective(updateRecord, example);
    }

    @Override
    public PayAlizftMchInfo selectBaseInfoById(Long subMchId) {
        return payAlizftMchInfoMapper.selectBaseInfoById(subMchId);
    }

    @Override
    public PayAlizftMchInfo selectBaseInfoByPPAIdAndSmId(Integer ppaId, String smid) {
        return payAlizftMchInfoMapper.selectBaseInfoByPPAIdAndSmId(ppaId, smid);
    }


    @Override
    public String getOneAvailableSmid(Long passageTemplateId,Long amount,
                               Long qRPaySupport, Long appPaySupport,
                              Long wapPaySupport, Long pcPaySupport
    ){
        return payAlizftMchInfoMapper.getOneAvailableSmid(passageTemplateId, amount, qRPaySupport, appPaySupport, wapPaySupport, pcPaySupport);
    }


    @Override
    public int updateDayAmount(String smid, Integer ppaId, Long amount){
        return payAlizftMchInfoMapper.updateDayAmount(smid, ppaId, amount);
    }

    @Override
    public List<Map> subData(int offset, int limit, Long passageTemplateId, String smid, String createTimeStart, String createTimeEnd) {
        Map param = new HashMap<>();
        param.put("offset",offset);
        param.put("limit",limit);
        if(StringUtils.isNotEmpty(smid)) param.put("smid", smid);
        if(passageTemplateId != null) param.put("passageTemplateId", passageTemplateId);
        if(StringUtils.isNotBlank(createTimeStart)) param.put("createTimeStart", createTimeStart);
        if(StringUtils.isNotBlank(createTimeEnd)) param.put("createTimeEnd", createTimeEnd);
        return payAlizftMchInfoMapper.subData(param);
    }

    @Override
    public Long subDataCount(String smid, Long passageTemplateId, String createTimeStart, String createTimeEnd) {
        Map param = new HashMap<>();
        if(StringUtils.isNotEmpty(smid)) param.put("smid", smid);
        if(passageTemplateId != null) param.put("passageTemplateId", passageTemplateId);
        if(StringUtils.isNotBlank(createTimeStart)) param.put("createTimeStart", createTimeStart);
        if(StringUtils.isNotBlank(createTimeEnd)) param.put("createTimeEnd", createTimeEnd);
        return payAlizftMchInfoMapper.subDataCount(param);
    }


    void setCriteria(PayAlizftMchInfoExample.Criteria criteria, PayAlizftMchInfo payAlizftMchInfo) {
        if(payAlizftMchInfo != null) {
            if(StringUtils.isNotEmpty(payAlizftMchInfo.getSmid())) criteria.andSmidEqualTo(payAlizftMchInfo.getSmid());
            if(payAlizftMchInfo.getRiskStatus() != null && payAlizftMchInfo.getRiskStatus() != -99) criteria.andRiskStatusEqualTo(payAlizftMchInfo.getRiskStatus());
            if(payAlizftMchInfo.getStatus() != null && payAlizftMchInfo.getStatus() != -99) criteria.andStatusEqualTo(payAlizftMchInfo.getStatus());
            if(payAlizftMchInfo.getApplyStatus() != null && payAlizftMchInfo.getApplyStatus() != -99) criteria.andApplyStatusEqualTo(payAlizftMchInfo.getApplyStatus());
            if(StringUtils.isNotEmpty(payAlizftMchInfo.getApplyOrderId() )) criteria.andApplyOrderIdEqualTo(payAlizftMchInfo.getApplyOrderId());
            if(payAlizftMchInfo.getSubMchId() != null && payAlizftMchInfo.getSubMchId() != -99) criteria.andSubMchIdEqualTo(payAlizftMchInfo.getSubMchId());
            if(payAlizftMchInfo.getPassageTemplateId() != null) criteria.andPassageTemplateIdEqualTo(payAlizftMchInfo.getPassageTemplateId());

            if(StringUtils.isNotEmpty(payAlizftMchInfo.getCertNo())) criteria.andCertNoEqualTo(payAlizftMchInfo.getCertNo());
        }
    }
}
